﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;
using Ext.Net;

namespace ExaminationSys.View.News
{
    public partial class NewsTypeList : System.Web.UI.Page
    {
        private static readonly NewsTypeService Proxy = ObjectFactory<NewsTypeService>.Create();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                var items = Proxy.GetAll();
                this.StoreNewsType.DataSource = items;
                this.StoreNewsType.DataBind();
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            this.NewsTypeEditWindow.GridStore = this.StoreNewsType;
        }
        protected void StoreNewsType_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            var items = Proxy.GetAll();
            this.StoreNewsType.DataSource = items;
            this.StoreNewsType.DataBind();
        }

        protected void ShowDetails(object sender, DirectEventArgs e)
        {
            string id = e.ExtraParams["id"];
            var realId = new Guid(id);
            var result = Proxy.Get(realId);
            if (result != null)
            {
                NewsTypeEditWindow.GridStore = StoreNewsType;
                this.NewsTypeEditWindow.SetData(result.Name, id, result.Description);
                this.NewsTypeEditWindow.Show();
            }
        }

        protected void Add_Click(object sender, DirectEventArgs e)
        {
            NewsTypeEditWindow.GridStore = StoreNewsType;
            this.NewsTypeEditWindow.SetData("", "", "");
            this.NewsTypeEditWindow.Show();
        }

        protected void Action_Click(object sender, DirectEventArgs e)
        {
            var selectModel = GridPanelNewsType.GetSelectionModel() as RowSelectionModel;
            if (selectModel == null)
            {
                MessageBoxHelper.ShowWarning("没有选中任何数据");
                return;

            }
            var id = selectModel.SelectedRow.RecordID;

            if (string.IsNullOrEmpty(id))
            {
                MessageBoxHelper.ShowWarning("获取ID失败");
                return;
            }

            var realId = new Guid(id);
            var item =Proxy.Get(realId);
            if (item != null)
            {
                item.Status = item.Status == 1 ? 0 : 1;
                var retCode = Proxy.Update(item);
                if (retCode)
                {
                    GridPanelNewsType.Reload();
                }
                else
                {
                    MessageBoxHelper.ShowError("保存失败");
                    return;
                }
            }

        }
    }
}